package com.fary.web.validator;

import cn.hutool.core.util.StrUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Map;

public class MapValueConstraintValidator implements ConstraintValidator<MapValue, Map<String, String>> {

    private String[] keys;

    @Override
    public void initialize(MapValue constraintAnnotation) {
        keys = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(Map<String, String> map, ConstraintValidatorContext constraintContext) {
        for (String key : keys) {
            if (StrUtil.isBlank(map.get(key))) {
                constraintContext.disableDefaultConstraintViolation();
                constraintContext.buildConstraintViolationWithTemplate(key + "不能为空！").addConstraintViolation();
                return false;
            }
        }
        return true;
    }


}
